//*********************************************************
//
//    Copyright (c) Microsoft. All rights reserved.
//    This code is licensed under the Apache License, Version 2.0.
//    THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
//    ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
//    IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
//    PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
//
//*********************************************************

using System;
using SR = Microsoft.Research.DataLayer;

namespace Microsoft.Research.ScientificWorkflow.ManagementStudioModels
{
    /// <summary>
    /// The model representing a View friendly representation for the schedule registry object.
    /// </summary>
    public class ScheduleViewModel
    {
        /// <summary>
        /// The scheduled workflow name.
        /// </summary>
        string workflowName;

        /// <summary>
        /// The job name.
        /// </summary>
        string jobName;

        /// <summary>
        /// Node name.
        /// </summary>
        string nodeName;

        /// <summary>
        /// The user who scheduled the workflow.
        /// </summary>
        string scheduledBy;

        /// <summary>
        /// The start time of the schedule.
        /// </summary>
        DateTime startTime;

        /// <summary>
        /// The end time of the schedule.
        /// </summary>
        DateTime endTime;

        /// <summary>
        /// The schedule type.
        /// </summary>
        SR.ScheduleType scheduleType;

        /// <summary>
        /// Check for expire enable.
        /// </summary>
        bool isExpireEnable;

        /// <summary>
        /// A user friendly description of the scheduled job.
        /// </summary>
        string description;

        /// <summary>
        /// The ID of the schedule object.
        /// </summary>
        Guid id;


        /// <summary>
        /// Constructor.
        /// </summary>
        /// <param name="schedule">The registry object.</param>
        public ScheduleViewModel(SR.JobSchedule schedule)
        {
            this.id = ((SR.IObject)schedule).ID;
            this.jobName = schedule.Name;
            this.nodeName = schedule.Template.Machines[0].Name;
            this.startTime = schedule.StartTime;
            this.endTime = schedule.StopTime;
            this.workflowName = schedule.Template.ActivityInstances[0].Activity.Name;
            this.scheduledBy = schedule.ScheduledBy.Name;
            this.scheduleType = schedule.Schedule;
            this.description = schedule.Description;
            this.isExpireEnable = schedule.IsExpireEnable;
        }

        /// <summary>
        /// 
        /// </summary>
        public Guid ScheduleId
        {
            get
            {
                return this.id;
            }
        }
        /// <summary>
        /// The worklflow being scheduled.
        /// </summary>
        public string WorkflowName
        {
            get { return this.workflowName; }
        }

        /// <summary>
        /// The job name.
        /// </summary>
        public string JobName
        {
            get { return this.jobName; }
        }

        /// <summary>
        /// The node where the workflow is scheduled.
        /// </summary>
        public string NodeName
        {
            get { return this.nodeName; }
        }

        /// <summary>
        /// The user who scheduled the workflow.
        /// </summary>
        public string ScheduledBy
        {
            get { return this.scheduledBy; }
        }

        /// <summary>
        /// Start time of the schedule.
        /// </summary>
        public DateTime StartTime
        {
            get { return this.startTime; }
        }

        /// <summary>
        /// End time of the schedule.
        /// </summary>
        public DateTime EndTime
        {
            get
            {
                if (!this.isExpireEnable)
                {
                    return DateTime.MinValue;
                }

                return this.endTime;
            }
        }

        /// <summary>
        /// The schedule type.
        /// </summary>
        public SR.ScheduleType ScheduleType
        {
            get { return this.scheduleType; }
        }

        /// <summary>
        /// A user friendly description of the scheduled job.
        /// </summary>
        public string Description
        {
            get { return this.description; }
        }

        /// <summary>
        /// Overridden tostring method. For automation Ids.
        /// </summary>
        /// <returns>The string representation of the object.</returns>
        public override string ToString()
        {
            if (!string.IsNullOrEmpty(this.jobName))
            {
                return this.jobName;
            }
            return base.ToString();
        }
    }
}
